<?php
/**
 * 礼物相关接口
 * */

namespace App\Http\Controllers;

use App\Http\Model\GiftLibrary;
use App\Http\Model\Hint;
use App\Http\Services\GiftServices;
use Illuminate\Http\Request;
use App\Lib\Response;

class GiftController extends BaseController
{
    protected $request;
    protected $service;
    protected $giveTypeConf = [1, 2]; //1.房间内 2.房间外

    public function __construct(GiftServices $service, Request $request)
    {
        $this->request = $request;
        $this->service = $service;
    }

    /**
     * @desc 礼物商品列表
     * @param $isShow
     * @param int $page
     * @param int $page_size
     * @date 2018/12/24
     * */
    public function gift_list()
    {
        $isShow = $this->request->get('is_show', 0);// 值为0时查询is_show=1的数据 值为1时查询所有数据
        $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
        $page_size = !empty((int)$this->request->get('page_size')) && $this->request->get('page_size') > 0 ? (int)$this->request->get('page_size') : 10;
        $room_id = $this->request->get('room_id', 0);
        $giftList = $this->service->getGiftList($isShow, $page, $page_size, $room_id);
        Response::returnResult(Hint::SUCCESS, $giftList);
    }

    /**
     * @desc 收入礼物列表
     * @param int $page
     * @param int $page_size
     * @date 2018/12/24
     * */
    public function obtain_gift_list()
    {
        $this->service->checkToken();
        $user_id = $this->request->get("user_id");
        if ($user_id == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
        $page_size = !empty((int)$this->request->get('page_size')) && $this->request->get('page_size') > 0 ? (int)$this->request->get('page_size') : 10;
        //获取礼物数量默认取10000条
        $page = 0;
        $page_size = 10000;
        $obtainList = $this->service->getObtainGiftList($user_id, $page, $page_size);

        Response::returnResult(Hint::SUCCESS, $obtainList);
    }

    /**
     * 获取预加载资源列表
     */
    public
    function get_animate_resources()
    {
        $data = $this->service->get_animate_resources();
        Response::returnResult(Hint::SUCCESS, $data);
    }


    /**
     * 房间内流水
     * @param Request $request
     */
    public function get_day_room_log(Request $request)
    {
        $room_id = $request->get('room_id');
        $page_index = $request->get('page_index');
        $page_size = $request->get('page_size');
        if (empty($room_id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (!isset($page_index)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (empty($page_size)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->service->get_day_room_log($room_id, $page_index, $page_size);
        Response::returnResult(Hint::SUCCESS, $data);
    }

################# 后台调用 ##########################################

    /**
     * 礼物更新
     * */
    public
    function giftUpdate()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'giftUpdate', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**获取互动礼物规则
     * @return array
     */
    public function getInteractRule(){
        do {
            if (!$this->checkRequest()) {
                break;
            }
            $this->callCommon($this->service, 'getInteractRule', $this->requestData);

        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    /**
     * Created at 2021/12/30 18:33
     * @Author <yuxiang.zhu@asiainnovations.net>
     */
    public function getGiftClassification()
    {
        Response::returnResult(Hint::SUCCESS, (new GiftLibrary())->getGiftClassification());
    }

}
